package com.woniuxy.service.impl;

import com.woniuxy.dao.RbacManagerDao;
import com.woniuxy.entity.RbacManager;
import com.woniuxy.service.RbacManagerService;
import com.woniuxy.utils.JWTUtil;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.stereotype.Service;
import sun.security.provider.MD5;
import sun.security.rsa.RSASignature;
import sun.security.util.Password;

import javax.annotation.Resource;
import java.security.CryptoPrimitive;
import java.util.List;
import java.util.Map;
import java.util.UUID;


/**
 * (RbacManager)表服务实现类
 *
 * @author makejava
 * @since 2021-04-25 23:04:54
 */
@Service("rbacManagerService")
public class RbacManagerServiceImpl implements RbacManagerService {
    @Resource
    private RbacManagerDao rbacManagerDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public RbacManager queryById(Integer id) {
        return this.rbacManagerDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @return 对象列表
     */
    @Override
    public List<Map<String,String>> queryAllByLimit() {
        return this.rbacManagerDao.queryAllByLimit();
    }

    /**
     * 新增数据
     *
     * @param rbacManager 实例对象
     * @return 实例对象
     */
    @Override
    public RbacManager insert(RbacManager rbacManager) {
        //shiro的加密方式 最后一位是盐值
        SimpleHash md5 = new SimpleHash("MD5", rbacManager.getPassword(), rbacManager.getAccount());
        String newPassword = md5.toHex();
        rbacManager.setPassword(newPassword);
        this.rbacManagerDao.insert(rbacManager);
        return rbacManager;
    }

    /**
     * 修改数据
     *
     * @param rbacManager 实例对象
     * @return 实例对象
     */
    @Override
    public RbacManager update(RbacManager rbacManager) {
        //shiro的加密方式
        SimpleHash md5 = new SimpleHash("MD5", rbacManager.getPassword(), rbacManager.getAccount());
        String newPassword = md5.toHex();
        rbacManager.setPassword(newPassword);
        this.rbacManagerDao.update(rbacManager);
        return this.queryById(rbacManager.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.rbacManagerDao.deleteById(id) > 0;
    }

    @Override
    public List<Map<String,String>> search(RbacManager rbacManager) {
        return this.rbacManagerDao.queryAll(rbacManager);
    }
}
